package tfts

import (
	"fmt"
	"strings"
)

type attrMeta struct {
	key        string
	health     int
	armor      int
	dmgTaken   int
	block      int // 伤害格挡
	healthAmp  int
	recoverAmp int
	shieldAmp  int
	healAmp    int
	hitRecover int
	factor     int // 倍数，默认100
	ap         int // 法强
	as         int // 攻速
	strike     int // 触发双重打击的概率
	castAmp    int // 施法增幅，基础为100
	hitMana    int // 攻击回复法力值
	manaAmp    int // 法力来源增幅，基础为0
	manaShrink int // 减少的最大魔法值，单位为百分比
	manaDec    int // 减少的最大魔法值，单位为1mana
}

func HitMana(i int) *attrMeta {
	return &attrMeta{hitMana: i, factor: 100}
}

func Block(i int) *attrMeta {
	return &attrMeta{block: i, factor: 100}
}

func DoubleStrike(i int) *attrMeta {
	return &attrMeta{strike: i, factor: 100}
}

func CastAmp(i int) *attrMeta {
	return &attrMeta{castAmp: i, factor: 100}
}

func ManaAmp(i int) *attrMeta {
	return &attrMeta{manaAmp: i, factor: 100}
}

func AP(i int) *attrMeta {
	return &attrMeta{ap: i, factor: 100}
}

func AS(i int) *attrMeta {
	return &attrMeta{as: i, factor: 100}
}

func HP(i int) *attrMeta {
	return &attrMeta{health: i, factor: 100}
}

func HpAmp(i int) *attrMeta {
	return &attrMeta{healthAmp: i, factor: 100}
}

func AR(i int) *attrMeta {
	return &attrMeta{armor: i, factor: 100}
}

func DR(i int) *attrMeta {
	return &attrMeta{dmgTaken: 100 - i, factor: 100}
}

func Hit(i int) *attrMeta {
	return &attrMeta{hitRecover: i, factor: 100}
}

func (am *attrMeta) String() string {
	if len(am.key) != 0 {
		return am.key
	}

	sb := strings.Builder{}

	// 处理 fighter 字段
	if am.health != 0 {
		sb.WriteString(fmt.Sprintf("Hp%d,", am.health))
	}
	if am.armor != 0 {
		sb.WriteString(fmt.Sprintf("Ar%d,", am.armor))
	}
	if am.block != 0 {
		sb.WriteString(fmt.Sprintf("Block%d,", am.block))
	}
	if am.dmgTaken != 0 {
		sb.WriteString(fmt.Sprintf("Dr%d,", 100-am.dmgTaken))
	}
	if am.healthAmp != 0 {
		sb.WriteString(fmt.Sprintf("HpAmp%d,", am.healthAmp))
	}
	if am.recoverAmp != 0 {
		sb.WriteString(fmt.Sprintf("RecoverAmp%d,", am.recoverAmp))
	}
	if am.shieldAmp != 0 {
		sb.WriteString(fmt.Sprintf("ShieldAmp%d,", am.shieldAmp))
	}
	if am.healAmp != 0 {
		sb.WriteString(fmt.Sprintf("HealAmp%d,", am.healAmp))
	}
	if am.hitRecover != 0 {
		sb.WriteString(fmt.Sprintf("HitRecover%d,", am.hitRecover))
	}

	// 处理 attributes 字段
	if am.factor != 100 {
		sb.WriteString(fmt.Sprintf("Fac%d,", am.factor))
	}
	if am.ap != 0 {
		sb.WriteString(fmt.Sprintf("Ap%d,", am.ap*am.factor/100))
	}
	if am.as != 0 {
		sb.WriteString(fmt.Sprintf("As%d,", am.as*am.factor/100))
	}
	if am.strike != 0 {
		sb.WriteString(fmt.Sprintf("Strike%d,", am.strike))
	}
	if am.castAmp != 0 {
		sb.WriteString(fmt.Sprintf("CastAmp%d,", am.castAmp))
	}
	if am.hitMana != 0 {
		sb.WriteString(fmt.Sprintf("HitMana%d,", am.hitMana))
	}
	if am.manaAmp != 0 {
		sb.WriteString(fmt.Sprintf("ManaAmp%d,", am.manaAmp))
	}

	// 检查并清除最后一个字符为逗号的情况
	if sb.Len() > 0 {
	}

	am.key = sb.String()

	if len(am.key) > 0 && am.key[len(am.key)-1] == ',' {
		am.key = am.key[:len(am.key)-1]
	}

	return am.key
}

func (am *attrMeta) Add(src *attrMeta) *attrMeta {
	if src.factor <= 0 {
		return am
	}
	if src.factor == 0 {
		src.factor = 100
	}
	am.health += src.health * src.factor / 100
	am.armor += src.armor * src.factor / 100
	if am.dmgTaken == 0 {
		am.dmgTaken = src.dmgTaken
	} else if src.dmgTaken > 0 {
		am.dmgTaken = am.dmgTaken * src.dmgTaken / 100
	}
	am.block += src.block * src.factor / 100
	am.healthAmp += src.healthAmp * src.factor / 100
	am.healAmp += src.healAmp * src.factor / 100
	am.shieldAmp += src.shieldAmp * src.factor / 100
	am.recoverAmp += src.recoverAmp * src.factor / 100
	am.ap += src.ap * src.factor / 100
	am.as += src.as * src.factor / 100
	am.strike += src.strike * src.factor / 100
	am.castAmp += src.castAmp * src.factor / 100
	am.hitMana += src.hitMana * src.factor / 100
	am.manaAmp += src.manaAmp * src.factor / 100
	am.manaShrink += src.manaShrink * src.factor / 100
	am.manaDec += src.manaDec * src.factor / 100
	return am
}

func (am *attrMeta) stat() *attrMeta {
	return am
}
